﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;
using WPF_CommunityToolkitMVVM_Simple.Model;

namespace WPF_CommunityToolkitMVVM_Simple.ViewModel
{
    /// <summary>
    /// MainWindow的ViewModel类
    /// 集合 请使用 ObservableCollection<MainViewModel>
    /// </summary>
    public partial class MainViewModel : ObservableObject
    {
        #region 控件 Binding
        /// <summary>
        /// 名字
        /// </summary>
        [ObservableProperty]
        private string _name = string.Empty;

        /// <summary>
        /// 邮箱
        /// </summary>
        [ObservableProperty]
        private string _email = string.Empty;

        /// <summary>
        /// 打招呼的日志
        /// </summary>
        [ObservableProperty]
        private string _log = string.Empty;
        #endregion 控件 Binding

        #region 控件事件
        /// <summary>
        /// 获取作者信息Command
        /// 数据流向：Model-> ViewModel-> View
        /// </summary>
        [RelayCommand]
        //public void ButtonFun(bool bool1)
        public void ButtonFun()
        {
            // Model ->ViewModel
            UserModel userModel = new UserModel()
            {
                ID = Guid.NewGuid().ToString("N"),
                Name = "Bili执笔小白",
                Email = "2806933146@qq.com"
            };

            #region 清空字符串（可省略）
            Name = string.Empty;
            Email = string.Empty;
            Log = string.Empty;
            #endregion 清空字符串（可省略）

            Name = userModel.Name;
            Email = userModel.Email;
        }

        //private RelayCommand<User>? buttonCommand;
        //public IRelayCommand<User> ButtonCommand => buttonCommand ??= new RelayCommand<bool>(ButtonFun);
        private RelayCommand? buttonCommand;
        public IRelayCommand ButtonCommand => buttonCommand ??= new RelayCommand(ButtonFun);


        /// <summary>
        /// 和作者打招呼Command - 异步
        /// 数据流向：ViewModel-> View
        /// </summary>
        [RelayCommand]
        public async Task Button1Fun()
        {
            await Task.Run(() =>
            {
                 Log = "你好！" + Name;
            });
        }

        private AsyncRelayCommand? button1Command;
        public IRelayCommand Button1Command => button1Command ??= new AsyncRelayCommand(Button1Fun);

        #region 控件事件-简写
        /// <summary>
        /// 加载、关闭
        /// </summary>
        [RelayCommand]
        public void Loaded()
        {
            Debug.WriteLine("Loaded");
        }
        [RelayCommand]
        public void Closing()
        {
            Debug.WriteLine("Closing");
        }
        [RelayCommand]
        public  async Task Closed()
        {
            await Task.Run(() =>
            {
                Debug.WriteLine("Closed");
            });
        }
        #endregion 控件事件-简写
        #endregion 控件事件
    }
}